﻿
using A.M.ETO;
using A.M.Storage.Application;
using L.M.Review.Application.IServices;
using L.M.Review.Domain.DTO;
using L.M.Review.Domain.Entity;
using L.M.Review.Infrastruecure.IRepository;
using L.M.Review.Infrastruecure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace L.M.Review.Application.Services
{
    public class Snfor_approval_formServices : BaseServices<infor_approval_form,string>, ISnfor_approval_formServices
    {
        private readonly ISnfor_approval_formRepository snfor_Approval_FormRepository;
        private readonly IProcess_instanceServices process_InstanceServices;
        private readonly IProcess_nodesServices process_NodesServices;
        private readonly IProcess_recordsRepository process_RecordsRepository;
        public Snfor_approval_formServices(ISnfor_approval_formRepository snfor_Approval_FormRepository, IProcess_nodesServices process_NodesServices, IProcess_instanceServices process_InstanceServices, IProcess_recordsRepository process_RecordsRepository) : base(snfor_Approval_FormRepository)
        {
            this.snfor_Approval_FormRepository = snfor_Approval_FormRepository;
            this.process_NodesServices = process_NodesServices;
            this.process_InstanceServices = process_InstanceServices;
            this.process_RecordsRepository = process_RecordsRepository;
        }

        /// <summary>
        /// 添加审批单 + 审批实例表 + 审批流转记录表
        /// </summary>
        /// <param name="addStaffInforDTO"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> AddStaffInfor(AddStaffInforDTO addStaffInforDTO)
        {
            try
            {
                ApiReturn<string> apiReturn = new ApiReturn<string>();
                snfor_Approval_FormRepository.BeginTranInfor();
                //添加审批单
                infor_approval_form infor_Approval_Form1 = new infor_approval_form();
                infor_Approval_Form1.approval_form_number = YitIdHelper.NextId().ToString();
                infor_Approval_Form1.reason_for_approval = addStaffInforDTO.reason_for_approval;
                infor_Approval_Form1.approval_project_number = addStaffInforDTO.approval_project_number;
                infor_Approval_Form1.appInfpr_Number = addStaffInforDTO.appInfpr_Number;
                infor_Approval_Form1.ShenPid = addStaffInforDTO.approval_project_number; 
                await snfor_Approval_FormRepository.AddInfor_Async(infor_Approval_Form1);
                //找到当前开始后第一个节点
                var res = await process_NodesServices.GetShowObjAsync(c => c.process_nodes_number == addStaffInforDTO.appInfpr_Number);
                var gets = await process_NodesServices.GetShowObjAsync(c => c.upper_node == res.lower_node);
                //找到下一个节点
                var lastes = await process_NodesServices.GetShowObjAsync(c => c.upper_node == gets.lower_node);
                //添加审批实例表
                process_instance process_Instance = new process_instance();
                var tid = YitIdHelper.NextId().ToString();
                process_Instance.process_instance_number = tid;
                process_Instance.current_node_number = gets.process_nodes_number;
                process_Instance.current_node_name = gets.process_nodes_name;
                process_Instance.remaining_processes = 0;
                process_Instance.process_initiator_number = addStaffInforDTO.process_initiator_number;
                process_Instance.process_initiator = addStaffInforDTO.process_initiator;
                process_Instance.operator_number = gets.process_nodes_role_number;
                process_Instance.operator_name = gets.process_nodes_role_name;
                process_Instance.to_da_role_number = lastes.process_nodes_number;
                process_Instance.to_da_role = lastes.process_nodes_name;
                process_Instance.application_form_number = addStaffInforDTO.application_form_number;
                process_Instance.XmInfor_id = addStaffInforDTO.approval_project_number;
                await process_InstanceServices.AddInfor_Async(process_Instance);
                snfor_Approval_FormRepository.CommitTranInfor();
                apiReturn.Message = "添加审批成功";
                return apiReturn;
            }
            catch (Exception)
            {
                snfor_Approval_FormRepository.RollbackTranInfor();
                throw;
            }
        }



    }
}
